package com.rtf.framework.dao.rule.levelpath;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface LevelPathRecordDao {

    @Select("<script>" +
            "SELECT level_path , level_name " +
            "FROM ${table_name} " +
            "WHERE id=#{id}" +
            "</script>")
    Map<String, String> queryById(@Param("table_name") String tableName,
                                        @Param("id") Long id);

    @Select("<script>" +
            "SELECT id , name , level_path , level_name , parent_id " +
            "FROM ${table_name} " +
            "WHERE level_path LIKE CONCAT('%',#{parentId},';%')" +
            "</script>")
    List<Map<String, Object>> querySubById(@Param("table_name") String tableName,
                      @Param("parentId") Long parentId);

    @Update("<script>" +
            "UPDATE ${table_name} " +
            "SET level_path=#{level_path} , level_name=#{level_name} " +
            "WHERE id=#{id}" +
            "</script>")
    int updateLevelPath(@Param("table_name") String tableName,
                        @Param("id") String id,
                        @Param("level_path") String levelPath, @Param("level_name") String levelName);

    @Select("<script>" +
            "SELECT count(1) " +
            "FROM ${table_name} " +
            "WHERE level_path LIKE CONCAT('%',#{id},';%')" +
            "</script>")
    int subNumByLevel(@Param("table_name") String tableName,
               @Param("id") String id) ;

    @Select("<script>" +
            "SELECT count(1) " +
            "FROM ${table_name} " +
            "WHERE parent_id =${id}" +
            "</script>")
    int subNumByParent(@Param("table_name") String tableName,
                    @Param("id") String id) ;
}
